package com.atguigu.cloud.resp;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author bairimengchang
 * @version 1.0
 * @project cloud2024
 * @description 统一定义返回对象
 * @date 2024/9/11
 */
@Data
@Accessors(chain = true)        // 可以这样写User user = new User().setName("张三").setAge(20);
public class ResultData<T> {

    // 加上泛型的原因是为了返回数据时，可以返回不同的对象，比如：List<User>、User、String等，类型检查
    private String code;/** 结果状态 ,具体状态码参见枚举类ReturnCodeEnum.java*/
    private String message;
    private T data;
    // 时间戳
    private long timestamp ;


    public ResultData (){
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> ResultData<T> success(T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(ReturnCodeEnum.RC200.getMessage());
        resultData.setData(data);
        return resultData;
    }

    public static <T> ResultData<T> fail(String code, String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMessage(message);
        return resultData;
    }

}
